﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScooterLand
{
    public partial class EmployeeForm : Form
    {
        Backend.Program controller = new Backend.Program();
        int employeeID = 0;

        public EmployeeForm()
        {
            InitializeComponent();
        }

        private void orderItem_Click(object sender, EventArgs e)
        {
            OrderForm oForm = new OrderForm();
            oForm.Show();
            this.Close();
        }

        private void customersItem_Click(object sender, EventArgs e)
        {
            CustomerForm cForm = new CustomerForm();
            cForm.Show();
            this.Close();
        }

        private void productsItem_Click(object sender, EventArgs e)
        {
            ProductForm pForm = new ProductForm();
            pForm.Show();
            this.Close();
        }

        private void employeesItem_Click(object sender, EventArgs e)
        {
            EmployeeForm eForm = new EmployeeForm();
            eForm.Show();
            this.Close();
        }

        private void invoicesItem_Click(object sender, EventArgs e)
        {
            InvoiceForm iForm = new InvoiceForm();
            iForm.Show();
            this.Close();
        }

        private void exitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EmployeeForm_Load(object sender, EventArgs e)
        {
            DataTable resultTable = controller.ListEmployees();
            DataRow[] resultRows = resultTable.Select();

            foreach (DataRow row in resultRows)
            {
                string[] str = { row["eid"].ToString(), row["fullname"].ToString(), row["initials"].ToString() };
                ListViewItem lvItem = new ListViewItem(str);
                lvEmployees.Items.Add(lvItem);
            }
        }

        private void btnSelectEmployee_Click(object sender, EventArgs e)
        {
            employeeID = Convert.ToInt32(lvEmployees.SelectedItems[0].Text);

            if (employeeID != 0)
            {
                lvBrands.Items.Clear();
                lvSpecialities.Items.Clear();

                string[] employeeArray = controller.GetEmployeeInfo(employeeID);

                txtEmployeeID.Text = employeeArray[0];
                txtFullName.Text = employeeArray[1];
                txtInitials.Text = employeeArray[2];

                // load not specialities
                DataTable brandTable = controller.ListBrands();
                DataRow[] brandRows = brandTable.Select();

                foreach (DataRow row in brandRows)
                {
                    string[] str = { row[0].ToString(), row[1].ToString() };
                    ListViewItem lvItem = new ListViewItem(str);
                    lvBrands.Items.Add(lvItem);
                }

                // load specialities
                DataTable specialTable = controller.ListSpecialities(employeeID);
                DataRow[] specialRows = specialTable.Select();

                foreach (DataRow row in specialRows)
                {
                    string[] str = { row[0].ToString(), row[1].ToString() };
                    ListViewItem lvItem = new ListViewItem(str);
                    lvSpecialities.Items.Add(lvItem);

                    // remove specialities from listview
                    lvBrands.Items.Remove(lvBrands.FindItemWithText(row[1].ToString()));
                }
            }
        }

        private void btnEmployeeSave_Click(object sender, EventArgs e)
        {
            bool result = false;

            if (employeeID != 0)
            {
                string fullName = txtFullName.Text;
                string initials = txtInitials.Text;
                
                result = controller.UpdateEmployee(employeeID, fullName, initials);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke gennemføres");
                else
                {
                    employeeID = 0;
                    txtEmployeeID.Text = "";
                    txtFullName.Text = "";
                    txtInitials.Text = "";

                    lvEmployees.Items.Remove(lvEmployees.SelectedItems[0]);

                    string[] str = { employeeID.ToString(), fullName, initials };
                    ListViewItem lvItem = new ListViewItem(str);
                    lvEmployees.Items.Add(lvItem);
                }
            }
            else
            {
                string fullName = txtFullName.Text;
                string initials = txtInitials.Text;

                controller.CreateEmployee(fullName, initials, out result, out employeeID);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke gennemføres");
                else
                {
                    DataTable brandTable = controller.ListBrands();
                    DataRow[] brandRows = brandTable.Select();

                    foreach (DataRow row in brandRows)
                    {
                        string[] str = { row[0].ToString(), row[1].ToString() };
                        ListViewItem lvItem = new ListViewItem(str);
                        lvBrands.Items.Add(lvItem);
                    }

                    string[] strEmployee = { employeeID.ToString(), fullName, initials };
                    ListViewItem lvEmployee = new ListViewItem(strEmployee);
                    lvEmployees.Items.Add(lvEmployee);
                }
            }
        }

        private void btnSpecialityAdd_Click(object sender, EventArgs e)
        {
            int AddSpeciality = Convert.ToInt32(lvBrands.SelectedItems[0].Text);
            bool result = controller.AddSpeciality(employeeID, AddSpeciality);

            if (result != true)
                MessageBox.Show("Handlingen kunne ikke gennemføres");
            else
            {
                string[] str = { AddSpeciality.ToString(), lvBrands.SelectedItems[0].SubItems[1].Text };
                ListViewItem lvItem = new ListViewItem(str);
                lvSpecialities.Items.Add(lvItem);

                lvBrands.Items.Remove(lvBrands.FindItemWithText(lvBrands.SelectedItems[0].SubItems[1].Text));
            }
        }

        private void btnSpecialityRemove_Click(object sender, EventArgs e)
        {
            int RemoveSpeciality = Convert.ToInt32(lvSpecialities.SelectedItems[0].Text);
            bool result = controller.RemoveSpeciality(employeeID, RemoveSpeciality);

            if (result != true)
                MessageBox.Show("Handlingen kunne ikke gennemføres");
            else
            {
                string[] str = { RemoveSpeciality.ToString(), lvSpecialities.SelectedItems[0].SubItems[1].Text };
                ListViewItem lvItem = new ListViewItem(str);
                lvBrands.Items.Add(lvItem);

                lvSpecialities.Items.Remove(lvSpecialities.FindItemWithText(lvSpecialities.SelectedItems[0].SubItems[1].Text));
            }
        }

        private void btnEmployeeRemove_Click(object sender, EventArgs e)
        {
            bool result = controller.RemoveEmployee(employeeID);

            if (result != true)
                MessageBox.Show("Handlingen kunne ikke gennemføres");
            else
            {
                lvEmployees.Items.Remove(lvEmployees.FindItemWithText(txtFullName.Text));

                employeeID = 0;
                lvBrands.Items.Clear();
                lvSpecialities.Items.Clear();
                txtEmployeeID.Text = "";
                txtFullName.Text = "";
                txtInitials.Text = "";
            }
        }

        private void lvBrands_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lvSpecialities_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
